import os, io
from heic2img import heic2img
        
from tkinter import *
import tkinter.messagebox as messagebox
from tkinter.filedialog import askopenfilename, askopenfilenames, askdirectory, asksaveasfilename

class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()

    def createWidgets(self):
        self.nameInput = Entry(self, width=50)
        self.nameInput.pack()
        self.nameInput.delete(0, 'end')
        self.nameInput.insert(0,'1.HEIC')
        self.alertButton = Button(self, text='select', command=self.sltfiles, height=2, width=6)
        self.alertButton.pack()
        self.nameInput2 = Entry(self, width=50)
        self.nameInput2.pack()
        self.nameInput2.delete(0, 'end')
        self.nameInput2.insert(0, 'png')
        self.alertButton2 = Button(self, text='convert', command=self.cvt, height=2, width=6)
        self.alertButton2.pack()

    def decode(name):
        if name[0]=='{' and name[-1]=='}':
            fl=name[1:-1].split('} {')
            return fl
        else:
            fl=name.split(' ')
            return fl

    def cvt(self):
        name = self.nameInput.get() or '1.HEIC'
        fmt = self.nameInput2.get() or 'jpeg'
        filelist=Application.decode(name)
        for fpi in filelist:
            print(f'convert {fpi}')
            heic2img(fpi, fmt)
        print('end cvt')

    def sltfiles(self):
        fd=askopenfilenames()
        self.nameInput.delete(0, 'end')
        self.nameInput.insert(0, fd)




 
if __name__ == "__main__":
    app = Application()
    app.master.title('convert heic')
    app.mainloop()
